package com.aqie.meet.ui.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libcommon.app.ToolbarActivity;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.api.file.FileModel;
import com.aqie.libfactory.model.api.user.UserComplainModel;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.helper.FileHelper;
import com.aqie.meet.ui.login.AccountActivity;

import java.io.File;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ComplainActivity extends ToolbarActivity {

    @BindView(R.id.et_content)
    EditText et_content;

    @BindView(R.id.iv_album)
    ImageView mAlbum;

    @BindView(R.id.upload_img)
    ImageView upload_img;

    @BindView(R.id.ll_media)
    LinearLayout ll_media;

    @BindView(R.id.ll_media_type)
    LinearLayout ll_media_type;

    //要上传的文件
    private File uploadFile = null;
    private String uploadFilePath = "";

    private String talentsId;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_complain;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        Intent intent = getIntent();
        talentsId = intent.getStringExtra("talentsId");
        setTitle("投诉界面");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // getMenuInflater().inflate(R.menu.input_menu, menu);
        // 初始化菜单
        MenuInflater inflater = getMenuInflater();
        // 加载布局到menu
        inflater.inflate(R.menu.submit, menu);
        MenuItem searchItem = menu.findItem(R.id.menu_input);
        searchItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // BaseApplication.showToast("点击菜单");
                submitComplain();
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    // 选择图片上传
    @OnClick(R.id.iv_album)
    void uploadPic(){
        FileHelper.getInstance().toAlbum(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case FileHelper.ALBUM_REQUEST_CODE:
                    if (data != null) {
                        Uri uri = data.getData();
                        String path = FileHelper.getInstance().
                                getRealPathFromURI(ComplainActivity.this, uri);
                        if (!TextUtils.isEmpty(path)) {
                            if (path.endsWith(".jpg")
                                    || path.endsWith(".png")
                                    || path.endsWith(".jpeg")) {

                            }
                            uploadFile = new File(path);
                            Bitmap bitmap = BitmapFactory.decodeFile(path);
                            upload_img.setImageBitmap(bitmap);
                            ll_media_type.setVisibility(View.GONE);
                            ll_media.setVisibility(View.VISIBLE);
                            uploadImgToNet();
                        }
                    }
            }
        }
    }

    // 单个文件上传
    private void uploadImgToNet() {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), uploadFile);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", uploadFile.getName(), requestFile);
        RemoteService service = Network.remote();
        Call<RspModel<FileModel>> call = service.upload(filePart);
        call.enqueue(new Callback<RspModel<FileModel>>() {
            @Override
            public void onResponse(Call<RspModel<FileModel>> call, Response<RspModel<FileModel>> response) {
                RspModel<FileModel> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("上传成功");
                    Log.e(TAG, body.getData().toString());
                    uploadFilePath = body.getData().getPath();
                    Log.e(TAG, uploadFilePath);
                }
            }

            @Override
            public void onFailure(Call<RspModel<FileModel>> call, Throwable t) {

            }
        });
    }

    // 提交投诉
    void submitComplain() {
        RemoteService service = Network.remote();
        UserComplainModel userComplainModel = new UserComplainModel();
        userComplainModel.setCid(talentsId);
        userComplainModel.setContent(et_content.getText().toString());
        userComplainModel.setImgList(Arrays.asList(uploadFilePath));
        // userComplainModel.setMsgIds(Arrays.asList("123","345"));

        Call<RspModel<Boolean>> call = service.complaintAdd(userComplainModel);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {
                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("举报成功");
                    finish();
                }else {
                    BaseApplication.showToast("举报失败 " + body.getMsg());
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {

            }
        });
    }
}